package com.example.viewtest.layout_9;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.viewtest.Utils;

/**
 * 设置默认的大小
 */
public class CircleView extends View {


    private static final int PADDING = (int) Utils.db2px(10);
    private static final int DEFAULT_RADIUS = (int) Utils.db2px(150);

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public CircleView(Context context) {
        super(context);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = (DEFAULT_RADIUS + PADDING) * 2;
        setMeasuredDimension(resolveSizeAndState(size, widthMeasureSpec, 0),
                resolveSizeAndState(size, heightMeasureSpec, 0));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.LTGRAY);
        int radius = (Math.min(getWidth(), getHeight()) - PADDING) / 2;
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
    }
}
